Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
main_freertos_two_tasks.c File Reference
+ Include dependency graph for main_freertos_two_tasks.c:

Go to the source code of this file.

Functions

static void read_task (void *pvParameters)
 
static void fusion_task (void *pvParameters)
 
int main (void)
 

Variables

SensorFusionGlobals sfg
 
ControlSubsystem controlSubsystem
 
StatusSubsystem statusSubsystem
 
PhysicalSensor sensors [3]
 
EventGroupHandle_t event_group = NULL
 

Detailed Description

This file shows one recommended way to incorporate sensor fusion capabilities into a FreeRTOS project.

Definition in file main_freertos_two_tasks.c.

Function Documentation

static void fusion_task ( void *  pvParameters)
static

Definition at line 120 of file main_freertos_two_tasks.c.

Referenced by main().

121 {
122  uint16_t i=0; // general counter variable
123  while (1)
124  {
125  xEventGroupWaitBits(event_group, /* The event group handle. */
126  B0, /* The bit pattern the event group is waiting for. */
127  pdTRUE, /* BIT_0 and BIT_4 will be cleared automatically. */
128  pdFALSE, /* Don't wait for both bits, either bit unblock task. */
129  portMAX_DELAY); /* Block indefinitely to wait for the condition to be met. */
130 
131  sfg.conditionSensorReadings(&sfg); // magCal is run as part of this
132  sfg.runFusion(&sfg); // Run the actual fusion algorithms
133  sfg.applyPerturbation(&sfg); // apply debug perturbation (testing only)
134 
135  sfg.loopcounter++; // The loop counter is used to "serialize" mag cal operations
136  i=i+1;
137  if (i>=4) { // Some status codes include a "blink" feature. This loop
138  i=0; // should cycle at least four times for that to operate correctly.
139  sfg.updateStatus(&sfg); // This is where pending status updates are made visible
140  }
141  sfg.queueStatus(&sfg, NORMAL); // assume NORMAL status for next pass through the loop
142  sfg.pControlSubsystem->stream(&sfg, sUARTOutputBuffer); // Send stream data to the Sensor Fusion Toolbox
143  }
144 }
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
EventGroupHandle_t event_group
streamData_t * stream
function to create packets for serial stream
Definition: control.h:73
struct ControlSubsystem * pControlSubsystem
#define B0
Definition: sensor_fusion.h:88
updateStatus_t * updateStatus
status=next status
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control.c:59
runFusion_t * runFusion
run the fusion routines
setStatus_t * queueStatus
queue status change for next regular interval
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
Operation is Nominal.
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.

+ Here is the caller graph for this function:

int main ( void  )

This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.

Definition at line 73 of file main_freertos_two_tasks.c.

74 {
75  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER_BLOCKING; // defined in the <shield>.h file
76 
77  BOARD_InitPins(); // defined in pin_mux.c, initializes pkg pins
78  BOARD_BootClockRUN(); // defined in clock_config.c, initializes clocks
79  BOARD_InitDebugConsole(); // defined in board.c, initializes the OpenSDA port
80 
81  I2Cdrv->Initialize(NULL); // Initialize the KSDK driver for the I2C port
82 
83  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); // Configure the I2C bus speed
84 
85  initializeControlPort(&controlSubsystem); // configure pins and ports for the control sub-system
86  initializeStatusSubsystem(&statusSubsystem); // configure pins and ports for the status sub-system
87  initSensorFusionGlobals(&sfg, &statusSubsystem, &controlSubsystem); // Initialize sensor fusion structures
88  // "install" the sensors we will be using
89  sfg.installSensor(&sfg, &sensors[0], FXOS8700_I2C_ADDR, 1, (void*) I2Cdrv, FXOS8700_Init, FXOS8700_Read);
90  sfg.installSensor(&sfg, &sensors[1], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, FXAS21002_Init, FXAS21002_Read);
91  sfg.installSensor(&sfg, &sensors[2], MPL3115_I2C_ADDR, 2, (void*) I2Cdrv, MPL3115_Init, MPL3115_Read);
92  sfg.initializeFusionEngine(&sfg); // This will initialize sensors and magnetic calibration
93 
94  event_group = xEventGroupCreate();
95  xTaskCreate(read_task, "READ", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
96  xTaskCreate(fusion_task, "FUSION", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
97 
98  sfg.setStatus(&sfg, NORMAL); // If we got this far, let's set status state to NORMAL
99  vTaskStartScheduler(); // Start the RTOS scheduler
100  sfg.setStatus(&sfg, HARD_FAULT); // If we got this far, FreeRTOS does not have enough memory allocated
101  for (;;) ;
102 }
void initSensorFusionGlobals(SensorFusionGlobals *sfg, StatusSubsystem *pStatusSubsystem, ControlSubsystem *pControlSubsystem)
utility function to insert default values in the top level structure
Definition: sensor_fusion.c:68
ControlSubsystem controlSubsystem
used for serial communications
int8_t FXOS8700_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
void initializeStatusSubsystem(StatusSubsystem *pStatus)
initializeStatusSubsystem() should be called once at startup to initialize the data structure and to ...
Definition: status.c:185
EventGroupHandle_t event_group
installSensor_t * installSensor
function for installing a new sensor into t
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
int8_t MPL3115_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
StatusSubsystem statusSubsystem
provides visual (usually LED) status indicator
int8_t FXOS8700_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
PhysicalSensor sensors[3]
This implementation uses three physical sensors.
int8_t FXAS21002_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t MPL3115_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t initializeControlPort(ControlSubsystem *pComm)
Initialize the control subsystem and all related hardware.
Definition: control.c:182
ARM_DRIVER_I2C * I2Cdrv
KSDK handle for the I2C port defined in Driver_I2C_KSDK2.c.
Non-recoverable FAULT = something went very wrong.
setStatus_t * setStatus
change status indicator immediately
int8_t FXAS21002_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
Operation is Nominal.
static void read_task(void *pvParameters)
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
static void fusion_task(void *pvParameters)

+ Here is the call graph for this function:

static void read_task ( void *  pvParameters)
static

Definition at line 104 of file main_freertos_two_tasks.c.

Referenced by main().

105 {
106  uint16_t i=0; // general counter variable
107  portTickType lastWakeTime;
108  const portTickType frequency = 1; // tick counter runs at the read rate
109  lastWakeTime = xTaskGetTickCount();
110  while (1)
111  {
112  for (i=1; i<=OVERSAMPLE_RATE; i++) {
113  vTaskDelayUntil(&lastWakeTime, frequency);
114  sfg.readSensors(&sfg, i); // Reads sensors, applies HAL and does averaging (if applicable)
115  }
116  xEventGroupSetBits(event_group, B0);
117  }
118 }
#define OVERSAMPLE_RATE
EventGroupHandle_t event_group
readSensors_t * readSensors
read all physical sensors
#define B0
Definition: sensor_fusion.h:88
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.

+ Here is the caller graph for this function:

Variable Documentation

ControlSubsystem controlSubsystem

used for serial communications

Definition at line 64 of file main_freertos_two_tasks.c.

EventGroupHandle_t event_group = NULL

Definition at line 67 of file main_freertos_two_tasks.c.

Referenced by fusion_task(), main(), and read_task().

PhysicalSensor sensors[3]

This implementation uses three physical sensors.

Definition at line 66 of file main_freertos_two_tasks.c.

This is the primary sensor fusion data structure.

Definition at line 63 of file main_freertos_two_tasks.c.

Referenced by FXOS8700_Init(), and initializeSensors().

StatusSubsystem statusSubsystem

provides visual (usually LED) status indicator

Definition at line 65 of file main_freertos_two_tasks.c.